const Sequelize = require('sequelize');
const DB = require("../config/dbconfig");
const userModel = require('./user_tab');

const chatModel = DB.define('chat_tab', {
    chat_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      primaryKey: true,
      comment: "聊天记录id，主键"
    },
    create_time: {
      type: Sequelize.DATE,
      allowNull: true,
      comment: "聊天记录时间"
    },
    content: {
      type: Sequelize.STRING(255),
      allowNull: true,
      comment: "聊天记录内容"
    },
    user1_id: {
      type: Sequelize.INTEGER,
      allowNull: true,
      comment: "用户表外键",
      references: {
        model: 'user_tab',
        key: 'user_id'
      }
    },
    user2_id: {
      type: Sequelize.INTEGER,
      allowNull: true,
      comment: "用户表外键"
    },
    is_delete:{
      type:Sequelize.INTEGER(2),
      comment:"聊天时间"
    }
  }, {
    Sequelize,
    tableName: 'chat_tab',
    timestamps: false,
    indexes: [
      {
        name: "PRIMARY",
        unique: true,
        using: "BTREE",
        fields: [
          { name: "chat_id" },
        ]
      },
      {
        name: "chat_user",
        using: "BTREE",
        fields: [
          { name: "user1_id" },
        ]
      },
    ]
  });
  chatModel.belongsTo(userModel, { foreignKey: 'user1_id', as: 'user1' }); 
  chatModel.belongsTo(userModel, { foreignKey: 'user2_id', as: 'user2' }); 

module.exports = chatModel
